package com.zhdl.hardware_contact.hardware_web.service;

import com.zhdl.hardware_contact.hardware_config.entity.ProjectConfiguration;
import com.zhdl.hardware_contact.hardware_param.entity.HardwareAbsPos;
import com.zhdl.hardware_contact.hardware_param.repository.HardwareAbsPosDao;
import com.zhdl.hardware_contact.hardware_param.service.impl.HardwareAbsPosServiceImpl;
import com.zhdl.hardware_contact.hardware_web.request.HardwareAbsPosReq;
import com.zhdl.hardware_contact.hardware_web.respone.HardwareAbsPosResp;
import com.zhdl.hardware_contact.hardware_web.respone.HardwareInfoViewResp;
import com.zhdl.modules.process.service.ProjectConfigProcessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.List;


@Slf4j
@Service

public class HardwareAbsService {

    @Resource
    private HardwareAbsPosServiceImpl hardwareAbsPosServiceImpl;

    @Resource
    private HardwareContactService hardwareContactService;

    @Resource
    private HardwareCurrentService hardwareCurrentService;
//    @Resource
//    private ProjectConfigProcessService projectConfigProcessService;

//    private Map<Integer,HardwareAbsPos> absPosMap = new HashMap<>();



//
//    /**
//     * 初始化 absPosMap
//     */
//    public void init(){
//        List<HardwareAbsPos> hardwareAbsPosList = hardwareAbsPosServiceImpl.selectAll();
//        for (HardwareAbsPos hardwareAbsPos : hardwareAbsPosList) {
//            absPosMap.put(hardwareAbsPos.getId(),hardwareAbsPos);
//        }
//    }

    /**
     * abs动作触发
     */
    public void controlHardwareRun(Integer absId){
//        Integer hardwareId = absPosMap.get(absId).getHardwareId();
        HardwareAbsPos hardwareAbsPos = hardwareAbsPosServiceImpl.selectById(absId);
        HardwareInfoViewResp hardwareInfo = hardwareContactService.getHardwareInfo(hardwareAbsPos.getHardwareId());
        String portD = "";
        String hardwareProtocol = hardwareInfo.getHardware_protocol();

        String type = hardwareInfo.getHardware_protocol_view().getType();
        if (type.equals("com")) {
            if (hardwareInfo.getHardware_protocol_view().getPortComm() != null) {
                portD = hardwareInfo.getHardware_protocol_view().getPortComm().getPort();
                log.info("写入的IP地址:{},,,{}", portD, hardwareProtocol);
            }
        } else {
            if (hardwareInfo.getHardware_protocol_view().getPortSocket() != null) {
                String clientIp = hardwareInfo.getHardware_protocol_view().getPortSocket().getServerIp();
                String clientPort = hardwareInfo.getHardware_protocol_view().getPortSocket().getServerPort();
                portD = clientIp + ":" + clientPort;
                log.info("写入的IP地址:{},,,{}", portD, hardwareProtocol);
            }
        }

        ProjectConfiguration projectConfiguration = hardwareContactService.getProjectConfigById(hardwareAbsPos.getAddressStartId());
        projectConfiguration.setValue("1");

        hardwareContactService.controlHardwareSendByProtocol(projectConfiguration, hardwareProtocol, portD);
        log.info(projectConfiguration.getAddress() + " : " + projectConfiguration.getValue());

    }


    /**
     * Abs位置保存
     */
    public void saveAbsPos(Integer absId , Integer hardwareId){
        HardwareAbsPos hardwareAbsPos = hardwareAbsPosServiceImpl.selectById(absId);
        ProjectConfiguration projectConfig = getCurrentPosValue(hardwareId);

        HardwareInfoViewResp hardwareInfo = hardwareContactService.getHardwareInfo(hardwareAbsPos.getHardwareId());
        String portD = "";

        String hardwareProtocol = hardwareInfo.getHardware_protocol();

        String type = hardwareInfo.getHardware_protocol_view().getType();
        if (type.equals("com")) {
            if (hardwareInfo.getHardware_protocol_view().getPortComm() != null) {
                portD = hardwareInfo.getHardware_protocol_view().getPortComm().getPort();
                log.info("写入的IP地址:{},,,{}", portD, hardwareProtocol);
            }
        } else {
            if (hardwareInfo.getHardware_protocol_view().getPortSocket() != null) {
                String clientIp = hardwareInfo.getHardware_protocol_view().getPortSocket().getServerIp();
                String clientPort = hardwareInfo.getHardware_protocol_view().getPortSocket().getServerPort();
                portD = clientIp + ":" + clientPort;
                log.info("写入的IP地址:{},,,{}", portD, hardwareProtocol);
            }
        }
        ProjectConfiguration projectConfiguration = hardwareContactService.getProjectConfigById(hardwareAbsPos.getAddressDistanceId());
        projectConfiguration.setValue(projectConfig.getValue());
        log.info(projectConfig.getField() + " "+projectConfig.getAddress()+ " : " + projectConfig.getValue());
        log.info(projectConfiguration.getField() + " "+projectConfiguration.getAddress()+ " : " + projectConfiguration.getValue());
        hardwareContactService.controlHardwareSendByProtocol(projectConfiguration,hardwareProtocol,portD);

    }





    /**
     * 更新硬件abs位置信息
     *
     * @param hardwareAbsPosReq
     */
    public void updateAbsPos(HardwareAbsPosReq hardwareAbsPosReq){
        {
            deleteHardwareAbsPos(hardwareAbsPosReq.getHardwareId());
            for (HardwareAbsPos hardwareAbsPos : hardwareAbsPosReq.getAbsPosList()) {
                hardwareAbsPos.setHardwareId(hardwareAbsPosReq.getHardwareId());
                saveHardwareAbsPos(hardwareAbsPos);
            }

        }

    }



    /**
     * 更新硬件abs位置信息
     *
     * @param absPosList,hardwareId
     */
        public void updateAbsPos(List<HardwareAbsPos> absPosList , Integer hardwareId){
        {
//            log.error(absPosList.toString());
            deleteHardwareAbsPosByhardwareId(hardwareId);
            if(!(absPosList==null|| absPosList.isEmpty())) {
                for (HardwareAbsPos hardwareAbsPos : absPosList) {
                    hardwareAbsPos.setHardwareId(hardwareId);
                    saveHardwareAbsPos(hardwareAbsPos);
                }
            }
        }

    }

    /**
     * 保存硬件Abs地址信息
     *
     * @param hardwareAbsPos
     * @return
     */
    public HardwareAbsPos saveHardwareAbsPos(HardwareAbsPos hardwareAbsPos){
        return hardwareAbsPosServiceImpl.insert(hardwareAbsPos);
    }

    /**
     * 更新硬件Abs地址信息
     *
     * @param hardwareAbsPos
     * @return
     */
    public HardwareAbsPos updateHardwareAbsPos(HardwareAbsPos hardwareAbsPos){
        return hardwareAbsPosServiceImpl.update(hardwareAbsPos,hardwareAbsPos.getId());
    }

    /**
     * 删除硬件Abs地址信息
     *
     * @param id
     */
    public void deleteHardwareAbsPos(Integer id){
        hardwareAbsPosServiceImpl.deleteById(id);
    }

    /**
     * 删除硬件Abs地址信息
     *
     * @param hardwareId
     */
    public void deleteHardwareAbsPosByhardwareId(Integer hardwareId){
        hardwareAbsPosServiceImpl.deleteByHardwareId(hardwareId);
    }

    /**
     * 查找硬件Abs地址信息 By HardwareId
     *
     * @param id
     * @return
     */
    public List<HardwareAbsPos> findHardwareAbsPosByHardwareId(Integer id){
       return hardwareAbsPosServiceImpl.findByHardwareId(id);
    }


    /**
     * 根据硬件Id获取实时位置数据
     *
     * @param hardwareId
     * @return
     */
    public ProjectConfiguration getCurrentPosValue(Integer hardwareId){
        for (ProjectConfiguration projectConfiguration : HardwareCurrentService.HardwareCurrentAddressMessageMap.get(hardwareId)) {
            if((projectConfiguration.getField().contains("Current")||projectConfiguration.getField().contains("current"))&& (projectConfiguration.getField().contains("Pos")||projectConfiguration.getField().contains("pos"))){
                return projectConfiguration;
            }
        }
        return null;
    }


    /**
     * 根据abs Id获取plc abs位置数据
     *
     * @param absId
     * @return
     */
    public String getAbsPosValue(Integer absId){
        HardwareAbsPos hardwareAbsPos = hardwareAbsPosServiceImpl.selectById(absId);
        ProjectConfiguration projectConfiguration = hardwareContactService.getProjectConfigById(hardwareAbsPos.getAddressDistanceId());
       return hardwareCurrentService.getProjectConfiguration(ProjectConfigProcessService.projectConfigurations,projectConfiguration).getValue();
    }



    public void getAbsPosValue(List<HardwareInfoViewResp> hardwareInfoAll){
        for (HardwareInfoViewResp hardwareInfoViewResp : hardwareInfoAll) {
            for (HardwareAbsPosResp hardwareAbsPosResp : hardwareInfoViewResp.getAbsPosList()) {
                hardwareAbsPosResp.setAbsPosValue(getAbsPosValue(hardwareAbsPosResp.getId()));
            }
        }
    }

}
